package com.larry.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

/**
 * @ProjectName hadoop
 * @Author larry
 * @Date 13:41 2021/11/18
 * @Description TODO
 *
 * 获取zookeeper客户端连接对象
 * 调用api
 *
 **/
public class ZookeeperTest {

    private ZooKeeper zooKeeper;

    /**
     * String connectString
     * int sessionTimeout
     * Watcher watcher
     */
    @Before
    public void init() throws IOException, InterruptedException {
        String connectString = "192.168.10.102:2181,192.168.10.103:2181,192.168.10.104:2181";
        int sessionTimeout = 10000;
        zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println(watchedEvent.toString() + watchedEvent);
            }
        });

    }

    @After
    public void close() throws InterruptedException {
        zooKeeper.close();
    }

    /**
     * 创建子节点
     * String path  节点路径
     * byte[] data  节点数据
     * List<ACL> acl    权限
     * CreateMode createMode    节点类型 -- 持久化 临时
     */
    @Test
    public void create() throws InterruptedException, KeeperException {
        zooKeeper.create("/sanguo","guanyu".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    /**
     * 获取子节点
     */
    @Test
    public void getChild() throws InterruptedException, KeeperException {
        List<String> children = zooKeeper.getChildren("/", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getPath());
            }
        });
        for (String child : children) {
            System.out.println(child);
        }
    }

    /**
     * 判断是否存在
     */
    @Test
    public void exist() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.exists("/sanguo", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getPath());
            }
        });
        if (stat == null){
            System.out.println("");
        }else {
            System.out.println("存在");
        }
    }

    /**
     * 获取节点数据
     * String path, Watcher watcher,DataCallback cb, Object ctx
     */
    @Test
    public void getData() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.exists("/sanguo", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getPath());
            }
        });
        byte[] data = zooKeeper.getData("/sanguo", false, stat);
        System.out.println("data" + "--------" + new String(data));
    }

    /**
     * 修改值
     */
    @Test
    public void setData() throws InterruptedException, KeeperException {
        Stat stat = zooKeeper.exists("/sanguo", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getPath());
            }
        });
        zooKeeper.setData("/sanguo", "liubei".getBytes(), stat.getVersion());
    }
}
